<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------

namespace app\user_center\model\user;

use think\Model;

/**
 * 用户关注 模型
 * @package app\user_center\model
 */
class UcMemberFollow extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'xm_uc_member_follow';

    public static function init(){
        self::event('after_insert', function ($user) {
            //被关注者 粉丝数 +1
            model('user_center/UcMemberCount', 'model\user') -> incFansTimes($user ->follow_uid);
            //关注者 关注数+1
            model('user_center/UcMemberCount', 'model\user') -> incFollowTimes($user ->uid);
        });
        self::event('after_delete', function ($user) {
            //被关注者 粉丝数 -1
            model('user_center/UcMemberCount', 'model\user') -> decFansTimes($user ->follow_uid);
            //关注者 关注数-1
            model('user_center/UcMemberCount', 'model\user') -> decFollowTimes($user ->uid);
        });
    }
	
	//关注渠道
    public function getFromTypeAttr($value){
        $status = [0=>'', 1=>'视频', 2=>'粉丝列表', 3=>'用户推荐', 4=>'粉丝通知'];
        return $status[$value];
    }
	
	/**
     * 获取当前用户关注信息
     * @param int $uid 			 	用户id
     * @param int $followid 		被关注者ID
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function getDataByFollow($uid, $follow_uid){
        $cacheName = 'uid_follow_'.$uid.'_'.$follow_uid;
		$map = [];
        $map['uid'] 		= $uid;
        $map['follow_uid'] 	= $follow_uid;
        $info = $this -> where($map) ->cache($cacheName)->find();
        if(!empty($info)){
            return array_out(200, 'success', $info);
        }else{
            return array_out(-1001, 'error');
        }
	}
	/**
     * 粉丝列表 及 关注人的信息
     * @param int $uid 			 	用户id
     * @param int $page 			页码
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function getListFansByUid($uid, $map = [], $order = '', $page = 1){
		if(!$order) $order = 'a.create_time desc';
		
		$map[] 	= ['a.follow_uid', '=', $uid];
        $list = self::alias('a') 
				-> leftjoin('uc_member b', 'a.uid = b.uid') 
				-> leftjoin('uc_member_follow c', 'a.follow_uid = c.uid AND c.follow_uid = a.uid') 
				-> field(['a.uid','b.nick_name','b.avatar_url','b.sex','b.autograph','b.member_status','c.id'=>'other','a.create_time' => 'create_time','a.from_type' => 'from_type']) 
				-> where($map) -> order($order) 
                -> paginate();
        return array_out(200, 'success', $list);
	}
	
	/**
     * 关注列表 及 被关注人的信息
     * @param int $uid 			 	用户id
	 * @param int $page 			页码
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function getListFollowByUid($uid, $map = [], $order = '', $page = 1){
		if(!$order) $order = 'a.create_time desc';
		
		$map[] 	= ['a.uid', '=', $uid];
		$list = self::alias('a') 
				-> leftjoin('uc_member b', 'a.follow_uid = b.uid') 
				-> leftjoin('uc_member_follow c', 'a.follow_uid = c.uid AND c.follow_uid = a.uid') 
				-> field(['a.follow_uid'=>'uid','b.nick_name','b.avatar_url','b.sex','b.autograph','b.member_status','c.id'=>'other','a.create_time' => 'create_time','a.from_type' => 'from_type']) 
				-> where($map) -> order($order) 
                -> paginate();
        return array_out(200, 'success', $list);
	}
	
	
	
	/**
     * 添加关注
     * @param int $uid 			 	用户id
     * @param int $follow_uid 		被关注者ID
     * @param int $from_type 		关注渠道
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function addData($uid, $follow_uid, $from_type){
		$this->uid  		= $uid;
		$this->follow_uid   = $follow_uid;
		$this->from_type   	= $from_type;
        $res = $this -> isUpdate(false) -> save();//添加  thinkphp主键bug
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	/**
     * 删除关注信息
     * @param int $uid 			 	用户id
     * @param int $follow_uid 		被关注者ID
     * @author shiqiang <976162041@qq.com>
     * @return array
     */
	public function deleteData($uid, $follow_uid){
		$map = [];
		$map['uid'] 		= $uid;
		$map['follow_uid'] 	= $follow_uid;
        $res = $this -> destroy($map);
        $cacheName = 'uid_follow_'.$uid.'_'.$follow_uid;
        \think\facade\Cache::rm($cacheName);
        if($res){
            return array_out(200, 'success');
        }else{
            return array_out(-1001, 'error');
        }
	}
	
	/**
     * 用户关注 - 我关注的 关联载入（一对一）
     */
    public function getFollowInfo(){
        return $this -> hasOne('app\user_center\model\user\Member','uid', 'follow_uid');
    }
	
	/**
     * 用户关注 - 关注我的 关联载入（一对一）
     */
    public function getFansInfo(){
        return $this -> hasOne('app\user_center\model\user\Member','uid', 'uid');
    }
}
